﻿
var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        tongzhilist: [],
        form: {
            ID: 0,
            pageindex: 0,
            pagesize: 10,
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        isRead: true,
        Status: '',
        checked: false,
        Read: false,
        showWin:false,
        isShowRead:false,
        touchID:0,//长按选中id
    },
    methods: {
        get_data: function (data, op) {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.pageindex = that.form.pageindex;
            options.pagesize = that.form.pagesize;
            options.IsReadonly = that.Read;
            options.action = "getmsglist";
            if (that.form.pageindex >= 10) {
                toast.loading({
                    title: "加载中",
                    duration: 2000
                }, function (ret) { });
            }
            ns.post(options, function (succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    // if (data.tongzhilist.length == that.form.pagesize) {
                    //     that.can_scroll = true;
                    // }
                    // if (that.form.pageindex == 0) {
                    //     that.tongzhilist =[];
                    //     that.firstID = data.tongzhilist[data.tongzhilist.length-1].ID;
                    //     setTimeout(() => {
                    //         window.scrollTo(0,document.body.scrollHeight);
                    //     }, 300);
                    // }
                    // data.tongzhilist.forEach(function(item){
                    //     that.tongzhilist.splice(0,0,item);
                    // })
                    // if(that.form.pageindex >= 10){
                    //     setTimeout(() => {
                    //         that.do_scrollbtn(that.tongzhilist[0].ID);
                    //         toast.hide();
                    //     }, 100);
                    // }
                    if (data.tongzhilist.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.tongzhilist = data.tongzhilist;
                    } else {
                        that.tongzhilist = that.tongzhilist.concat(data.tongzhilist);
                    }

                    app.current_item_length = that.tongzhilist.length;
                    for (var i = 0; i < data.tongzhilist.length; i++) {
                        data.tongzhilist[i].AddTime = ns.SetTime_style(data.tongzhilist[i].AddTime);
                    }
                    if (that.form.ID) {
                        setTimeout(function () {
                            that.do_scrollTo();
                        }, 1000)
                    }
                    api.sendEvent({
                        name: 'isreadTongzhi'
                    })
                } else if (err) {
                    that.tongzhilist = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },

        //点击遮罩关闭弹窗
        doHideWin:function(){
            var that=this;
            that.showWin=false;
            that.touchID=0;
            that.get_data();
        },
        //长按弹出操作框
        gettouchStart:function(index,JpushLogID,IsRead){
            var that = this;
            api.addEventListener({
                name:'longpress'
            }, function(ret){  
                that.showWin=!that.showWin; 
                if(!IsRead){
                    that.isShowRead=true;
                }else{
                    that.isShowRead=false;
                }
                that.touchID=JpushLogID;
            })
        },
        do_Read:function(){
            var that = this;
        },
        //翻页第一条数据的位置
        do_scrollbtn: function (id) {
            var sollid = document.getElementById('scroll' + id);
            window.scrollTo(0, sollid.offsetTop);
        },

        choose_noread: function () {
            var that = this;
            that.Read = !that.Read;
            that.get_data();
            // that.checked=!that.checked;
            // console.log(JSON.stringify(that.checked));
        },
        All_read: function () {
            var that = this;
            var options = {};
            options.action = 'msgmessageread';
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        open_detail: function (item) {
            var that = this;
            ns.openWin('tongzhixiaoxidetail_frm', '消息详情', {
                id: item.ID
            });
            api.sendEvent({
                name: 'isreadtrue',
                extra: {
                    key1: 'value1'
                }
            });
        },
        //搜索结果返回位置
        do_scrollTo: function () {
            var that = this;
            var sollid = document.getElementById('scroll' + that.form.ID);
            window.scrollTo(0, sollid.offsetTop);
            var classlist = document.getElementById('scroll' + that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function () {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
        },
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.form.ID = api.pageParam.ID || 0;
    app.get_data();
    // ns.init_pull(true,true);
    ns.init_pull();
    api.addEventListener({
        name: 'isreadtrue'
    }, function (ret, err) {
        if (ret) {
            setTimeout(function () {
                app.get_data();
            }, 1000);
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });
        }
    });
     //搜索
     api.addEventListener({
        name: 'do_open_order_search'
    }, function (ret, err) {
        ns.openWin('home_newssousuo', '搜索');
    });
    //清空
    api.addEventListener({
        name: 'do_open_delete'
    }, function (ret, err) {
        api.confirm({
            title: '提示信息',
            msg: '确认要清理所有的消息信息吗？',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            if (ret) {
                if (ret.buttonIndex == 1) {
                    var options={};
                    options.action="";
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                            that.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '删除中'
                    });
                }
            }
        })
    });
};
